import React, {Component} from "react";
import {Card, Form, Modal, Select} from "antd";
import PropTypes from "prop-types";
import {SearchForm, TableContent, OpenWinProductSpecs, OpenWinProducer} from "../../../widgets";
import {getOrganNo, getDomain, getArchId} from "../../../utils";
import {accDiv} from '../../../utils/Acc'
import {storageRecordMarketSea} from "../../../utils/api";
import BreadcrumbCustom from "../../../components/BreadcrumbCustom";

const FormItem = Form.Item;
const Option = Select.Option;

export default class extends Component {

    static contextTypes = {
        router: PropTypes.object
    }

    constructor(props) {
        super(props);
        this.table = undefined
        this.openWinProductSpecs = undefined
        this.seaSpecNum = 1
        this.state = {
            isSingle: true,
            producerOrganNo: undefined,
            searchParam: {},
            showProducer: true
        }
        this.isProducer = getDomain() == 'WINDER-PRODUCER'
        this.seaFields = [{
            label: '类别',
            key: 'seaType',
            initData: 'single',
            render: (props) => {
                return (
                    <Select style={{width: 120}} onChange={this.selectChange}>
                        <Option value="single">单支</Option>
                        <Option value="batch">整件</Option>
                    </Select>
                )
            }
        }, {
            label: '厂方',
            key: 'producer',
            hide: this.isProducer,
            componentFunc: (props) => {
                return (
                    <OpenWinProducer callback={(v) => {
                        this.setState({
                            producerOrganNo: v['producerOrganNo']
                        }, () => {
                            console.log('empty openWinProductSpecs')
                            this.openWinProductSpecs.empty()
                            this.openWinProductSpecs.setTableSeaExtendVal({
                                organNo: v['producerOrganNo']
                            })
                        })
                    }} {...props}/>
                )
            }
        },{
            label: '品名',
            key: 'prCode',
            componentFunc: (props) => (
                <OpenWinProductSpecs ref={e => {this.openWinProductSpecs = e}} callback={(v = {}) => {
                    this.seaSpecNum = parseInt(v['specsNum'] || 1)
                }} {...props} />
            )
        }, {
            label: '批号',
            key: 'batchNum'
        }]
    }

    componentDidMount() {
        if(this.isProducer) {
            this.setState({
                producerOrganNo: getOrganNo()
            }, () => {
                this.openWinProductSpecs.empty()
                this.openWinProductSpecs.setTableSeaExtendVal({
                    organNo: this.state.producerOrganNo
                })
            })
        }
    }

    selectChange = (v) => {
        if(v == 'single') {
            this.setState({
                isSingle: true
            })
        } else {
            this.setState({
                isSingle: false
            })
        }
    }



    seaBtns = []

    columns = [{
        title: '日期',
        dataIndex: 'formattedActionTime',
        key: 'formattedActionTime'
    }, {
        title: '仓库',
        key: 'storageName',
        render: (t, record) => {
            return record['storageName'] == undefined ? "默认仓库" : record['storageName']
        }
    },{
        title: '类型',
        key: 'recordType',
        render: (t, record) => {
            const text = record['recordType'] || ""
            return (text == 1  && "入库") || (text == 2 && "出库") || (text == 3 && "退货")
        }
    },{
        title : '客户',
        key: 'linkCustomer',
        dataIndex: 'linkCustomer'
    },{
        title: '品名',
        dataIndex: 'erCodeName',
        key: 'erCodeName'
    }, {
        title: '批号',
        dataIndex: 'erBatchData',
        key: 'erBatchData'
    }, {
        title: '箱号',
        dataIndex: 'erMaterialBox',
        key: 'erMaterialBox'
    }]

    render() {
        let newSeaFileds = []
        if(this.state.isSingle) {
            newSeaFileds = this.seaFields.concat([{
                label: '流水号',
                key: 'flowNo'
            }])
        } else {
            newSeaFileds = this.seaFields.concat([{
                label: '箱号',
                key: 'boxNo'
            }])
        }
        return (
            <div>
                <BreadcrumbCustom first="市场管理" second="货物查询" />
                <Card title="货物查询" bordered={false}>
                    <SearchForm fields={newSeaFileds} btns={this.seaBtns} onSearch={this.onSearch}/>
                    <TableContent
                        noLoad={true}
                        ref={(e) => {this.table = e}}
                        columns={this.columns}
                        url = {storageRecordMarketSea}
                        searchParam={this.state.searchParam}
                    />
                </Card>
            </div>
        )
    }

    onSearch = (values) => {
        let erCode = ''
        if(values['batchNum'].length <=2) {
            Modal.error({
                title: '提示',
                content: '批号格式有误'
            })
            return false
        }
        values['batchNum'] = values['batchNum'].substring(2)
        if(this.state.isSingle) {
            let flowNo = Math.ceil(accDiv(values['flowNo'], this.seaSpecNum))
            let boxNo = (Array(6).join('0') + flowNo).slice(-6)
            console.log('flowNo:', flowNo, 'boxNo:', boxNo, 'seaSpecNum', this.seaSpecNum)
            erCode = values['prCode'] + values['batchNum'] + boxNo
        } else {
            erCode = values['prCode'] + values['batchNum'] + values['boxNo']
            console.log('values: ', values)
        }
        values['erCode'] = erCode
        values['isSingle'] = this.state.isSingle
        // values['recordType'] = 1
        if(this.isProducer) {
            values['producerOrganNo'] = getOrganNo()
        } else {
            values['producerOrganNo'] = this.state.producerOrganNo
            values['dealerOrganNo'] = getOrganNo()
        }

        this.setState({
            searchParam: Object.assign({}, this.state.searchParam, values)
        }, () => {
            this.table.loadData()
        })
    }
}
